﻿var timeout = 300;
var closetimer = null;
var openMenus = new Array();
$(document).ready(setupMenuEvents);

function setupMenuEvents() {
    $(".SubMenuParent").mouseenter(function (eventObj) {
        var t = $(this);
        var tparent = t.offsetParent(".SubMenu").prev(".SubMenuParent");
        var bheight = $(window).height();
        var bwidth = $(window).width();
        var bhScr = $(window).scrollLeft();
        var bvScr = $(window).scrollTop();
        closeMenus(tparent, t);
        addMenuInArray(t);
        if (!t.hasClass("active"))
            t.addClass("active");
        var cm = t.next(".SubMenu");
        var cmw = cm.width(), cmh = cm.height();

        if (t.hasClass("HorzMenu")) {
            if (t.offset().left + t.width() + cmw > (bwidth + bhScr))
                cm.css("left", t.position().left - cmw);
            else
                cm.css("left", t.position().left + t.width())
            cm.css("top", t.position().top);

        }
        else {
            if ((t.offset().top + t.height() + cmh > (bheight + bvScr)) && (t.position().top - cmh >= 0))
                cm.css("top", t.position().top - cmh);
            else
                cm.css("top", t.position().top + t.height());

            if (t.offset().left + cmw > (bwidth + bhScr)) {
                if (t.offset().left + t.width() > (bwidth + bhScr))
                    cm.css("left", bwidth + bhScr - cmw);
                else
                    cm.css("left", t.position().left + t.width() - cmw);
            }
            else
                cm.css("left", t.position().left)
        }

        //debugger;
        if (cm.hasClass("fadeMenu"))
            cm.fadeIn("def");
        else if (cm.hasClass("slideMenu"))
            cm.slideDown("def");
        else if (cm.hasClass("toggleMenu"))
            cm.toggle("def");
        else
            cm.css("display", "block");
        menuItemOpen = true;
        eventObj.stopPropagation();
    }).mouseleave(function (eventObj) {
        var s = $(eventObj.relatedTarget).offsetParent(".SubMenu").prev(".SubMenuParent");
        var tp = $(this).offsetParent(".SubMenu").prev(".SubMenuParent");
        if (s.length == 0 || s.get(0) != tp.get(0)) {
            setMenuTimer();
        }
        else if (s.get(0) == tp.get(0)) {
            cancelTimer();
            closetimer = window.setTimeout(function () { closeMenus(tp); }, timeout);
        }
        else {
            closeMenus(s);
        }
    });
    $(".SubMenu").mouseenter(function (eventObj) {
        closeMenus($(this).prev(".SubMenuParent").eq(0));
        eventObj.stopPropagation();

    }).mouseleave(function (eventObj) {
        setMenuTimer();
    });
}

var menuItemOpen = false;
function setMenuTimer() {
    cancelTimer();
    closetimer = window.setTimeout("closeMenus()", timeout);
}
function closeMenus(inMenParent, inMen) {
    if (typeof inMenParent == "number")
        inMenParent = null;
    cancelTimer();
    for (var i = openMenus.length - 1; i >= 0; i--) {
        if (inMenParent == null || inMenParent == 0 || inMenParent.get(0) != openMenus[i].get(0)) {
            if (inMen != null && inMen.get(0) == openMenus[i].get(0))
                break;
            openMenus[i].removeClass("active");
            var cm = openMenus[i].next(".SubMenu");
            if (cm.hasClass("fadeMenu"))
                cm.fadeOut("def");
            else if (cm.hasClass("slideMenu"))
                cm.slideUp("def");
            else if (cm.hasClass("toggleMenu"))
                cm.toggle("def");
            else
                cm.css("display", "none");
            openMenus.pop();
        }
        else if (inMenParent.get(0) == openMenus[i].get(0))
            break;
    }
    if (openMenus.length == 0)
        menuItemOpen = false;

}
function cancelTimer() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}
function addMenuInArray(men) {
    for (var i = 0; i < openMenus.length - 1; i++)
        if (men.get(0) == openMenus[i].get(0))
            return;
    openMenus.push(men);
}
